#include <windows.h>
#include <windowsx.h>

#include "myWindow.h"
#include "myD3D.h"

#define window true
#define scr_w 640
#define scr_h 480

#define cFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)
struct cVert {float x,y,z; D3DVECTOR norm; DWORD color;};

#define texFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
struct texVert {float x,y,z; D3DVECTOR normal; float u,v;};


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int){

	// create variables for window and directX
	myWindow myWnd;
	myD3D md3d;

	float angle = 0;

	// init window
	if (!myWnd.myCreateWindow(hInst, L"DX 3D Engine...", !window, scr_w, scr_h))
		return 10;

	// init directX
	if (!md3d.Init(myWnd.getWndHandle(), !window, scr_w, scr_h))
		return 20;

	md3d.light3d(true);

	D3DXMATRIX tr1, tmp;
	D3DXMatrixIdentity(&tr1);


	md3d.addXFile(L"map.x", NULL);
	//md3d.addXFile(L"arm.x", &tr1);
	if (!md3d.addXAnimatedMeshFile(L"arm.x"))
		return 30;
	

	// turn off the default light
	md3d.setLight(0, false, NULL);

	// add point light with linear attenuation
	D3DLIGHT9 l;
	ZeroMemory(&l, sizeof l);
	l.Type = D3DLIGHT_POINT;
	l.Diffuse.a = l.Diffuse.r = l.Diffuse.g = l.Diffuse.b = 1.0f;
	//l.Attenuation1 = 0.05f;
	l.Range = 4000.0f;
	l.Position = D3DXVECTOR3(0, 0, 10.0f);
	md3d.setLight(0, true, &l);


	// load shader file
	if (!md3d.loadShaderFile(L"second.fx"))
		return 50;
	
	
	// enter an infinite message loop
	MSG msg;
	while (true){
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			if(WM_QUIT == msg.message)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		//update modifications
		//D3DXMatrixTranslation(&tmp, 0, 10.0f, 0);
		D3DXMatrixRotationY(&tr1, D3DXToRadian(angle));
		//tr1 = tr1 * tmp;
		if (angle >=(360*10)) { angle = 0; } else { angle+=0.5f; }
		// render frame
		md3d.RenderFrame();
	}

	return msg.wParam;
}